package com.biyabi.controller;

import com.biyabi.pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@RestController
@RequestMapping("/back/file")
public class FileController {
    @RequestMapping("/uploadLogo")
    public Result upload(MultipartFile logoFile, HttpServletRequest request){
        Result result = new Result();
        try{
            String realPath = request.getSession().getServletContext().getRealPath("/upload/logo");
            logoFile.transferTo(new File(realPath+"/"+logoFile.getOriginalFilename()));
            result.setStatus(true);
            result.setMessage("/upload/logo/"+logoFile.getOriginalFilename());
        }catch(Exception e){
            result.setStatus(false);
            result.setMessage("上传失败！");
        }
        return result;
    }
    @RequestMapping("/uploadPicture")
    public Result uploadPicture(MultipartFile logoFile, HttpServletRequest request){
        Result result = new Result();
        try{
            String realPath = request.getSession().getServletContext().getRealPath("/upload/picture");
            logoFile.transferTo(new File(realPath+"/"+logoFile.getOriginalFilename()));
            result.setStatus(true);
            result.setMessage("/upload/picture/"+logoFile.getOriginalFilename());
        }catch(Exception e){
            result.setStatus(false);
            result.setMessage("上传失败！");
        }
        return result;
    }
    @RequestMapping("/uploadCover")
    public Result uploadCover(MultipartFile logoFile, HttpServletRequest request){
        Result result = new Result();
        try{
            String realPath = request.getSession().getServletContext().getRealPath("/upload/cover");
            logoFile.transferTo(new File(realPath+"/"+logoFile.getOriginalFilename()));
            result.setStatus(true);
            result.setMessage("/upload/cover/"+logoFile.getOriginalFilename());
        }catch(Exception e){
            result.setStatus(false);
            result.setMessage("上传失败！");
        }
        return result;
    }
    @RequestMapping("/uploadImage")
    public Result uploadImage(MultipartFile logoFile, HttpServletRequest request){
        Result result = new Result();
        try{
            String realPath = request.getSession().getServletContext().getRealPath("/upload/image");
            logoFile.transferTo(new File(realPath+"/"+logoFile.getOriginalFilename()));
            result.setStatus(true);
            result.setMessage("/upload/image/"+logoFile.getOriginalFilename());
        }catch(Exception e){
            result.setStatus(false);
            result.setMessage("上传失败！");
        }
        return result;
    }
}
